/********************************************
 * 功能说明: LesseeContact实体与DTO之间接口数据转换实现
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2020-03-24 10:09:23
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.operation.tenant.web.converter;

import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.operation.tenant.repository.LesseeContactRepository;
import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.operation.tenant.errorcode.TenantErrorCode;
import com.spring.arch.operation.tenant.model.LesseeContact;
import com.spring.arch.operation.tenant.repository.LesseeContactRepository;
import com.spring.arch.operation.tenant.web.dto.WebLesseeContactDTO;
import lombok.NonNull;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;

/**
 * LesseeContact实体与DTO之间接口数据转换实现
 * @author
 * @version 1.0.0.1
 * @since 2020-03-24 10:09:23
 */
@Component
public class WebLesseeContactConverter extends AbstractConverter<LesseeContact, WebLesseeContactDTO> {

    @Autowired
    private LesseeContactRepository lesseeContactRepository;
    @Autowired
    private EntityManager entityManager;

    @Override
    public LesseeContact toModel(@NonNull final WebLesseeContactDTO dto) {
        if (dto.isNew()) {
            return constructModel(dto);
        } else {
            return updateModel(dto);
        }
    }

    @Override
    public WebLesseeContactDTO toDTO(@NonNull final LesseeContact model) {
        final WebLesseeContactDTO dto = new WebLesseeContactDTO();

        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setIdCard(model.getIdCard());
        dto.setMobilePhoneNumber(model.getMobilePhoneNumber());
        dto.setMailAddress(model.getMailAddress());
        dto.setPostalAddress(model.getPostalAddress());
        dto.setDefaultContact(model.isDefaultContact());

        this.convertCommonToDTO(model, dto);

        return dto;
    }

    /**
     * 构建新Model
     */
    private LesseeContact constructModel(final WebLesseeContactDTO dto) {
        final LesseeContact model = new LesseeContact();
        this.dtoToModel(dto, model);
        return model;
    }

     /**
      * 更新Model
      */
    private LesseeContact updateModel(final WebLesseeContactDTO dto) {
        final LesseeContact model = this.lesseeContactRepository.findById(dto.getId())
                        .orElseThrow(() -> ExceptionHolder.serviceException(TenantErrorCode.E1201000001, dto.getId().toString()));
        entityManager.unwrap(Session.class).evict(model);
        this.dtoToModel(dto, model);
        return model;
    }

    private void dtoToModel(final WebLesseeContactDTO dto, final LesseeContact model) {
        model.setName(dto.getName());
        model.setIdCard(dto.getIdCard());
        model.setMobilePhoneNumber(dto.getMobilePhoneNumber());
        model.setMailAddress(dto.getMailAddress());
        model.setPostalAddress(dto.getPostalAddress());
        model.setDefaultContact(dto.isDefaultContact());
    }
}
